const http = require('./../commons/http');
const config = require('./../config');
const logger = require('./../commons/logger');
exports.getUserInfo = async (ctx, code) => {
  if (ctx.session.person) {
    return Promise.resolve(ctx.session.person);
  } else {
    let ret = await http.get(ctx, config.restapi.backend + '/user/code/' + code);
    if (ret.code === 10000) {
      ctx.session.person = ret;
    }
    return Promise.resolve(ret);
  }
}

exports.getMyTask = (ctx, query) => {
  let ret = http.get(ctx, config.restapi.backend + '/enlist/list', { params: query});
  return ret;
}

exports.confirmTask = (ctx, enlistId) => {
  let ret = http.post(ctx, config.restapi.backend + '/activity/customer/confirm', enlistId);
  return ret;
}

exports.cancelTask = (ctx, enlistId) => {
  let ret = http.post(ctx, config.restapi.backend + '/activity/customer/cancel', enlistId);
  return ret;
}

exports.getAppointments = (ctx, query) => {
  logger.info(query);
  let ret = http.get(ctx, config.restapi.backend + '/plan/list', { params: query});
  return ret;
}

exports.ensureAccount = (ctx, ifo) => {
  let ret = http.post(ctx, config.restapi.backend + '/user/bind', {
    ...ifo,
    openId: ctx.session.person&&ctx.session.person.data.openId
  });
  return ret;
}

exports.getRecords = (ctx, query) => {
  let ret = http.get(ctx, config.restapi.backend + '/enroll/list', { params: query});
  return ret;
}

exports.getSchedules = (ctx, query) => {
  let ret = http.get(ctx, config.restapi.backend + '/scheduler/list', { params: query});
  return ret;
}

exports.sendEnsureCode = (ctx, mobile) => {
  let ret = http.get(ctx, config.restapi.backend + '/user/sendVecode', { params: {
    openId: ctx.session.person&&ctx.session.person.data.openId,
    mobile: mobile
  }});
  return ret;
}

exports.updateName = (ctx, name) => {
  let ret = http.post(ctx, config.restapi.backend + '/user/fillName', {
    name,
    personId: ctx.session.person&&ctx.session.person.data.personId
  });
  return ret;
}

exports.getContacts = (ctx, params) => {
  let ret = http.post(ctx, config.restapi.backend + '/activity/enlist/persons', params);
  return ret;
}

exports.newAppointment = (ctx, params) => {
  let ret = http.post(ctx, config.restapi.backend + '/activity/customer/enlist', params);
  return ret;
}

exports.getAccommodation = (ctx, id) => {
  let ret = http.get(ctx, config.restapi.backend + '/enroll/'+id+'/checkInInfo');
  return ret;
}

exports.sign = (ctx, params) => {
  params.personId = ctx.session.person&&ctx.session.person.data.personId;
  let ret = http.post(ctx, config.restapi.backend + '/activity/customer/sign', params);
  return ret;
}